package servlet;

import domain.NingboIncomeExpenditure;
import domain.ZhejiangIncomeExpenditure;
import org.json.JSONArray;
import org.json.JSONObject;
import service.NingboIEService;
import service.ZhejiangIEService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ZhejiangIEServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ZhejiangIEService zjies = new ZhejiangIEService();
        request.setCharacterEncoding("UTF-8");
        List<ZhejiangIncomeExpenditure> list = zjies.selectDataByCityAndYear(request.getParameter("address")
            , Integer.parseInt(request.getParameter("year")));
        JSONArray jsonArray = new JSONArray();
        for(ZhejiangIncomeExpenditure zjie : list) {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("type", zjie.getType());
            jsonObj.put("value", zjie.getValue());
            jsonObj.put("urban_rural", zjie.getUrbanRural());
            jsonArray.put(jsonObj);
        }
        response.setHeader("content-type","text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(jsonArray.toString());
    }
}
